package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsAlarmBtWindow;
import com.zhike.service.DwsAlarmBtWindowService;
import com.zhike.mapper.DwsAlarmBtWindowMapper;
import com.zhike.utils.BarUtils;
import com.zhike.utils.FormUtils;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_alarm_bt_window(车型品牌告警表)】的数据库操作Service实现
* @createDate 2024-05-01 21:05:55
*/
@Service
public class DwsAlarmBtWindowServiceImpl extends ServiceImpl<DwsAlarmBtWindowMapper, DwsAlarmBtWindow>
    implements DwsAlarmBtWindowService{

    @Autowired
    private DwsAlarmBtWindowMapper dwsAlarmBtWindowMapper;

    @Override
    public String getAlarmTypeToday(String filter) {
        String date = BarUtils.getDate(filter);
        List<String> carType = new ArrayList<>();
        List<Long> alarm = new ArrayList<>();
        List<Long> alarmL1 = new ArrayList<>();
        List<Long> alarmL2 = new ArrayList<>();
        List<Long> alarmL3 = new ArrayList<>();
        List<Map> series = new ArrayList<>();

        try {
            List<Map> alarmTypeToday = dwsAlarmBtWindowMapper.getAlarmTypeToday(date);
            if (!alarmTypeToday.isEmpty()) {
                for (Map map : alarmTypeToday) {
                    carType.add(map.get("car_type").toString());
                    alarm.add(Long.parseLong(map.get("alarm_count").toString()));
                    alarmL1.add(Long.parseLong(map.get("alarm_count_l1").toString()));
                    alarmL2.add(Long.parseLong(map.get("alarm_count_l2").toString()));
                    alarmL3.add(Long.parseLong(map.get("alarm_count_l3").toString()));
                }
            }

            series.add(Map.of(
                    "name","三级告警次数",
                    "data",alarmL3
            ));
            series.add(Map.of(
                    "name","二级告警次数",
                    "data",alarmL2
            ));

            series.add(Map.of(
                    "name","一级告警次数",
                    "data",alarmL1
            ));
            series.add(Map.of(
                    "name","告警次数",
                    "data",alarm
            ));


            Map data = Map.of(
                    "categories", carType,
                    "series", series
            );
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAlarmBrandToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgTypeToday = dwsAlarmBtWindowMapper.getAlarmBrandToday(date);
            Map data = FormUtils.getDeail(avgTypeToday,"品牌","brand");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAlarmTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        List<String> carType = new ArrayList<>();
        List<Long> alarm = new ArrayList<>();
        List<Long> alarmL1 = new ArrayList<>();
        List<Long> alarmL2 = new ArrayList<>();
        List<Long> alarmL3 = new ArrayList<>();
        List<Map> series = new ArrayList<>();

        try {
            List<Map> alarmTypeToday = dwsAlarmBtWindowMapper.getAlarmTypeWeek(date);
            if (!alarmTypeToday.isEmpty()) {
                for (Map map : alarmTypeToday) {
                    carType.add(map.get("car_type").toString());
                    alarm.add(Long.parseLong(map.get("alarm_count").toString()));
                    alarmL1.add(Long.parseLong(map.get("alarm_count_l1").toString()));
                    alarmL2.add(Long.parseLong(map.get("alarm_count_l2").toString()));
                    alarmL3.add(Long.parseLong(map.get("alarm_count_l3").toString()));
                }
            }

            series.add(Map.of(
                    "name","三级告警次数",
                    "data",alarmL3
            ));
            series.add(Map.of(
                    "name","二级告警次数",
                    "data",alarmL2
            ));

            series.add(Map.of(
                    "name","一级告警次数",
                    "data",alarmL1
            ));
            series.add(Map.of(
                    "name","告警次数",
                    "data",alarm
            ));

            Map data = Map.of(
                    "categories", carType,
                    "series", series
            );
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAlarmBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgTypeToday = dwsAlarmBtWindowMapper.getAlarmBrandWeek(date);
            Map data = FormUtils.getDeail(avgTypeToday,"品牌","brand");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

}




